(define nodesGen
    (lambda (lista)
        (if (null? lista)
            '()
            (cons (cons (caar lista) -1) (nodesGen (cdr lista)))
        )
    )
)

(define visited
    (lambda (lista k)
        (if (null? lista)
            #f
            (if (eq? (caar lista) k)
                (begin
                  (if (= (cdar lista) (- 1))
                    #f
                    #t
                  )
                )
                (visited (cdr lista) k)
            )
        )
    )
)

(define visit
    (lambda (lista id val)
        (if (null? lista)
            '()
            (if (eq? (caar lista) id)
                (cons (cons (caar lista) val) (visit (cdr lista) id val)) 
                (cons (cons (caar lista) (cdar lista))(visit (cdr lista) id val))
            )
        )
    )
)

(define BFS
    (lambda (G s e)
      (define queue (list s))
      (define nodes (nodesGen G))
      (define modNeighbour
        (lambda (id val)
          (display id)
          (display #\newline)
          (display val)
          (display #\newline)
          (if (not (visited nodes id))
              (begin
                (set! queue (append queue (list id)))
                (display queue)
                (set! nodes (visit nodes id val))
              )
          )
          (display #\newline)
        )
      )
      (define modNeighbours
        (lambda (neighbours)
          (if (not (null? neighbours))
            (begin
              (modNeighbour (caar neighbours) (cadar neighbours))
              (modNeighbours (cdr neighbours))
            )
          )
        )
      )
      (define bfsHelp
        (lambda (G e)
          (if(null? queue) "not found"
            (if(eq? (car queue) e) "found"
              (begin
                (display #\newline)
                (display queue)
                (display #\newline)
                (display (cdr queue))
                (modNeighbours (car (cdr (assq (car queue) G))))
                (set! queue (cdr queue))
                (bfsHelp G e)
              )
            )
           )
        )
      )
      (display "foo\n")
      (display (bfsHelp G e))
      (display nodes)
    )
)

(begin
  (display "BFS:\n")
  (BFS   '((S ((A 3) (D 4)))
         (A ((S 4) (D 5) (B 4)))
         (D ((S 4) (A 5) (E 2)))
         (B ((A 4) (E 5) (C 4)))
         (E ((D 2) (A 5) (B 5) (F 4)))
         (C ((B 4)))
         (F ((E 4) (G 3)))
         (G ((F 3)))
         ) 'S 'G)
  )